package com.yoomiito.app.ui.anyview.shareview

import android.content.Context
import android.graphics.Bitmap
import android.view.LayoutInflater
import android.view.View
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.SimpleTarget
import com.bumptech.glide.request.transition.Transition
import com.google.zxing.WriterException
import com.yoomiito.app.R
import com.yoomiito.app.utils.L
import com.yoomiito.app.utils.ResUtil
import com.yoomiito.app.utils.ZxingUtil

abstract class BaseShareView(val context: Context) {
    var onPicMakeListener:(View,Bitmap)->Unit={view, bitmap ->  }
    lateinit var view:View

    fun parentView(layoutId:Int){
       view = LayoutInflater.from(context).inflate(layoutId,null,false)
    }

    abstract fun makePic()
    abstract fun initView()
    fun getBitmapFromUrl(url:String,corner:Int = 0,bitmap:(Bitmap)->Unit){
        if (corner>0){
            val corners = RoundedCorners(corner)
            val options = RequestOptions.bitmapTransform(corners)
            Glide.with(context).asBitmap().apply(options).load(url).into(object : SimpleTarget<Bitmap>() {
                override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
                    bitmap(resource)
                }
            })
        }else{
            L.i("开始加载图片")
            Glide.with(context).asBitmap().load(url).into(object : SimpleTarget<Bitmap>() {
                override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
                    L.i("加载图片结束，开始回调：$bitmap")
                    bitmap(resource)
                    L.i("回调结束")
                }
            })
        }
    }

    fun getQRCode(shareUrl: String):Bitmap =  ZxingUtil.cretaeZxing(shareUrl, ResUtil.getColor(R.color.color_black))
}